package www.openteam.com.adapter;

import android.content.Context;
import android.support.v7.util.SortedList;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import www.openteam.com.R;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.ProductInfo;

/**
 * Created by kylin on 2016/9/26.
 */

public class ProductAdapter extends RecyclerView.Adapter {


    private static final String TAG = "ProductAdapter";
    private LayoutInflater mLayoutInflater;
    private SortedList<ProductInfo.DataEntity> mSortedList;
    private ArrayList<CustomType.DataBean> materialTypelsts =new ArrayList<CustomType.DataBean>();
    private Context  context;
    private BuildProductInfoIn buildProductInfoIn;
    private int type=0;

    public ProductAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(ProductInfo.DataEntity.class, new SortedList.Callback<ProductInfo.DataEntity>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(ProductInfo.DataEntity o1, ProductInfo.DataEntity o2) {

                if (Integer.parseInt(o1.getRownumber()) < Integer.parseInt(o2.getRownumber())) {
                    return -1;
                } else if (Integer.parseInt(o1.getRownumber()) > Integer.parseInt(o2.getRownumber())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(ProductInfo.DataEntity oldItem, ProductInfo.DataEntity newItem) {
                Log.d(TAG, "areContentsTheSame: " + 1);     boolean statue  ;
                if (oldItem.getId() == newItem.getId() &&
                        oldItem.getType().equals(newItem.getType())&&
                        oldItem.getCustCode().equals(newItem.getCustCode())&&
                        oldItem.getCode().equals(newItem.getCode())){
                    statue=true;
                }else {
                    statue=false;
                }
                return statue;
            }

            @Override
            public boolean areItemsTheSame(ProductInfo.DataEntity item1, ProductInfo.DataEntity item2) {
                boolean statue  ;
                if (item1.getId() == item2.getId() &&
                        item1.getType().equals(item2.getType())&&
                        item1.getCustCode().equals(item2.getCustCode())&&
                        item1.getCode().equals(item2.getCode())){
                    statue=true;
                }else {
                    statue=false;
                }
                return statue;

            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }

    public void addItems(ArrayList<ProductInfo.DataEntity> list) {
        mSortedList.beginBatchedUpdates();
        for (ProductInfo.DataEntity itemModel : list) {
            mSortedList.add(itemModel);
        }
        mSortedList.endBatchedUpdates();

    }
    public void addItemsM(  ArrayList<CustomType.DataBean> materialTypelst,Context context) {


        for (CustomType.DataBean mt:materialTypelst
             ) {
            materialTypelsts.add(mt);
        }
        this.context =context;
    }

    public void deleteItems(ArrayList<ProductInfo.DataEntity> items) {
        mSortedList.beginBatchedUpdates();
        for (ProductInfo.DataEntity item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new ProductAdapter.mViewHolder(mLayoutInflater.inflate(R.layout.item_product, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//        Log.d(TAG, "DataAdapter:4");
        ProductAdapter.mViewHolder viewHolder = (ProductAdapter.mViewHolder) holder;
            ProductInfo.DataEntity item = mSortedList.get(position);
        for (int i = 0; i < materialTypelsts.size(); i++) {
            if (materialTypelsts.get(i).getCode().equals(item.getType())) {
                type = i;

            }

        }
        viewHolder.tv_prorow.setText("\t\t\t\t\t"+item.getRownumber());
        Log.d(TAG, "onBindViewHolder: " +item.getId());
//        viewHolder.spinner_product.setAdapter(new MyAdapter(materialTypelsts,this.context));
        viewHolder.spinner_product.setText(materialTypelsts.get(type).getName());
        viewHolder.tv_productno.setText(item.getId()+"");
        viewHolder.tv_productname.setText(item.getName());
        viewHolder.tv_customno_product.setText(item.getCustCode());
        viewHolder.tv_len_product.setText(item.getLength());
        viewHolder.tv_wid_product.setText(item.getWidth());
        viewHolder.tv_hei_product.setText(item.getHeight());
        viewHolder.tv_format_product.setText(item.getSpecsUnit());
        viewHolder.tv_text_product.setText(item.getContentpage()+"");
        viewHolder.tv_conver_product.setText(item.getCoverPage()+"");
        viewHolder.unit_product.setText(item.getSpecs());
        viewHolder.tv_maxstock.setText(item.getMaxstock());
        viewHolder.tv_minstock.setText(item.getMinstock());
        viewHolder.btn_new_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buildProductInfoIn.newBuildProductInfo();
            }
        });
        viewHolder.btn_modify_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buildProductInfoIn.updateProductInfo(mSortedList.get(position),position);
            }
        });
        viewHolder.btn_delete_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buildProductInfoIn.deleteProductInfo(mSortedList.get(position).getCode());
            }
        });

    }


    public  void  buildProductInfoInRealize(BuildProductInfoIn buildProductInfoIn){
                this.buildProductInfoIn=buildProductInfoIn;
    }


    public interface BuildProductInfoIn{
            void newBuildProductInfo( );
            void updateProductInfo(ProductInfo.DataEntity production,int position);
            void deleteProductInfo(String code);
    }

    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    static class mViewHolder extends RecyclerView.ViewHolder {

        TextView spinner_product;
        TextView tv_productno;
        TextView tv_productname;
        TextView tv_customno_product;
        TextView tv_len_product;
        TextView tv_wid_product;
        TextView tv_hei_product;
        TextView tv_format_product;
        TextView tv_text_product;
        TextView tv_conver_product;
        TextView unit_product;
        TextView tv_maxstock;
        TextView tv_minstock;
        TextView tv_prorow;
        Button btn_new_product,btn_modify_product,btn_delete_product;


        public mViewHolder(View itemView) {
            super(itemView);
//            Log.d(TAG, "DataAdapter: 5");
            spinner_product = (TextView) itemView.findViewById(R.id.spinner_product);
            tv_productno = (TextView) itemView.findViewById(R.id.tv_productno);
            tv_productname = (TextView) itemView.findViewById(R.id.tv_productname);
            tv_customno_product= (TextView) itemView.findViewById(R.id.tv_customno_product);
            tv_len_product= (TextView) itemView.findViewById(R.id.tv_len_product);
            tv_wid_product= (TextView) itemView.findViewById(R.id.tv_wid_product);
            tv_hei_product= (TextView) itemView.findViewById(R.id.tv_hei_product);
            tv_format_product = (TextView) itemView.findViewById(R.id.tv_format_product);
            tv_text_product = (TextView) itemView.findViewById(R.id.tv_text_product);
            tv_conver_product = (TextView) itemView.findViewById(R.id.tv_conver_product);
            tv_conver_product = (TextView) itemView.findViewById(R.id.tv_conver_product);
            unit_product = (TextView) itemView.findViewById(R.id.unit_product);
            tv_minstock = (TextView) itemView.findViewById(R.id.tv_minstock);
            tv_maxstock = (TextView) itemView.findViewById(R.id.tv_maxstock);
            tv_prorow= (TextView) itemView.findViewById(R.id.tv_prorow);
            btn_new_product= (Button) itemView.findViewById(R.id.btn_new_product);
            btn_modify_product= (Button)itemView.findViewById(R.id.btn_modify_product);
            btn_delete_product=(Button)itemView.findViewById(R.id.btn_delete_product);
        }
    }



    class MyAdapter extends BaseAdapter {

        List<CustomType.DataBean> mList =null;
        Context mContext =null;
        String code= null;

        public MyAdapter(List mList, Context mContext) {
            this.mList = mList;
            this.mContext = mContext;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);
            convertView=_LayoutInflater.inflate(R.layout.item_user, null);
            if(convertView!=null) {
                TextView tv= (TextView) convertView.findViewById(R.id.spinner_item);
                tv.setText(mList.get(position).getName());
            }
            return convertView;
        }
    }
}